package com.example.gl_project5;

public class VertexShader {
	private String shader;
	
	public VertexShader() {
		shader = "uniform mat4 MVPMatrix;      \n" 
				  + "uniform mat4 MVMatrix;      \n"
				  + "uniform vec4 ambientLight; \n"
				  + "attribute vec4 vVertex;       \n"
				  + "attribute vec3 a_Normal;       \n"
				  
				  + "struct lightProperties \n"
				  + "{ \n"
				  + "vec3 position; \n"
				  + "vec4 color; \n"
				  + "mediump float attenuation0; \n"
                  + "mediump float attenuation1; \n"
                  + "mediump float attenuation2; \n"
				  + "}; \n"
		 
		          
				  
				  + "struct materialProperties \n" 
				  + "{ \n"
                  + "vec4  ambientColor; \n"
                  + "vec4  diffuseColor; \n"
                  + "vec4  specularColor; \n"
                  + "mediump float specularExponent; \n"
                  + "}; \n"
 
				  + "uniform lightProperties light1; \n"
                  + "uniform materialProperties material; \n"
				 
				  + "varying vec4 vVaryingColor;          \n"     
				 
				  + "void main()                    \n"  
				  + "{                              \n"
				  + "   vec4 aColor  = material.ambientColor*ambientLight;            \n"     
				  
				  + "   vec3 modelViewVertex = vec3(MVMatrix * vVertex);              \n"
				  
				  + "   vec3 modelViewNormal = vec3(MVMatrix * vec4(a_Normal, 0.0));     \n"
				  + "   mediump float distance = length(light1.position - modelViewVertex);             \n"
				  + "   vec3 lightVector = normalize(light1.position - modelViewVertex);        \n"
				  + "   mediump float diffuse = max(dot(modelViewNormal, lightVector), 0.0);       \n"
				  + "   mediump float intensity = 1.0 / (light1.attenuation0 + light1.attenuation1 * distance + (light1.attenuation2 * distance * distance));  \n"
				  + "   vec4 dColor = material.diffuseColor*diffuse*intensity*light1.color; \n"
				  
				  + "   vec4 sColor = vec4(0.0,0.0,0.0,1.0);"
				  + "   if(diffuse>0.0){ \n"
				  + "    float specular =max(0.0, dot(lightVector, modelViewNormal)); \n"
				  + "    float fspecular = pow(specular, material.specularExponent);  \n"
				  + "    sColor = intensity*fspecular*light1.color*material.specularColor; \n"
				  + "   } \n"
				  
				  + "   vVaryingColor = aColor + dColor + sColor; \n"
				  + "   vVaryingColor.a = 1.0; \n"
				  
				//  + "   vVaryingColor = vec4(a_Normal,1.0);\n"
				 // + "   vVaryingColor = vec4(light1.attenuation0*10.0,0.0,0.0,1.0);\n"
				 //   + "   vVaryingColor = light1.color;\n"
				
				    
				  + "   gl_Position = MVPMatrix*vVertex;   \n"     
				  + "}                              \n"; 
	}
	
	public String toString() {
		return shader;
	}
}
